package com.jmcgg.freesky.common.util;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.jmcgg.freesky.common.dao.FreeskyPOJO;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Title: FreeSky Common Java Library <br>
 * Description: FreeSky Common Java Library <br>
 * Copyright: Copyright (c) 2005~2009 www.afss.cn <br>
 * Company: Amei's FreeSky Studio <br>
 * 
 * @author <b>Jacky Chou</b> (AFSS [Amei's FreeSky Studio])
 * @version 1.2.1, 2008-08-05, Clean and optimize the source code.<br>
 *          1.2.0, 2008-02-23, Upgrade freesky.jar to 1.2.0 with Struts 2<br>
 *          1.0.0, 2006-05-30, Initial Version<br>
 */
public class FreeskyAction extends ActionSupport {
	private static final long serialVersionUID = 1222208818717538923L;

	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(FreeskyAction.class);

	// _view, _new, _add, _edit, _update, _delete, _page, _all, _list, _noop
	protected String _tag; // web operation type
	protected String curSite; // current site
	protected String _msg; // message to be displayed on page
	protected ArrayList<FreeskyPOJO> voList; // returned list
	protected String page; // page number
	protected boolean guestPage; // everyone can access this page
	protected final static String ERR_CLOSE = "ERR_CLOSE";
	protected final static String SUC_CLOSE = "SUC_CLOSE";
	protected final static String PAGE_LOGIN = "PAGE_LOGIN";
	protected final static String PAGE_ERROR = "PAGE_ERROR";

	public String get_tag() {
		return _tag;
	}

	public String getCurSite() {
		return curSite;
	}

	public void setCurSite(String curSite) {
		this.curSite = curSite;
	}

	public ArrayList<FreeskyPOJO> getVoList() {
		return voList;
	}

	public void setVoList(ArrayList<FreeskyPOJO> voList) {
		this.voList = voList;
	}

	public void set_tag(String _tag) {
		this._tag = _tag;
	}

	public String get_msg() {
		return _msg;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public void set_msg(String _msg) {
		this._msg = _msg;
	}

	protected int getCurPage() {
		if (page == null) {
			return 0;
		} else {
			try {
				return Integer.parseInt(page);
			} catch (NumberFormatException e) {
				return 0;
			}
		}
	}

	protected HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}

	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * Show Alert message on the browser.
	 * 
	 * @param response
	 * @param msg
	 */
	public static void showAlert(HttpServletResponse response, String msg) {
		try {
			String uniMsg = CodeUtil.codeISO2Uni(msg);
			String alertStr = "<script language=\"javascript\">alert(\""
					+ uniMsg + "\");</script>";
			response.getWriter().println(alertStr);

		} catch (IOException e) {
			logger.error(
					"showAlert(HttpServletResponse, String) - Error occurs when show alert message - msg="
							+ msg, e);
		}
	}

	@Override
	public String execute() throws Exception {
		String redirectPage = Const.WEB_OP_NOOP;
		doBefore();
		if (_tag == null)
			return doNoop();
		if (!validateUser()) {
			return PAGE_LOGIN;
		}
		if (_tag.equals(Const.WEB_OP_VIEW)) {
			redirectPage = doView();
		} else if (_tag.equals(Const.WEB_OP_NEW)) {
			redirectPage = doNew();
		} else if (_tag.equals(Const.WEB_OP_ADD)) {
			redirectPage = doAdd();
		} else if (_tag.equals(Const.WEB_OP_EDIT)) {
			redirectPage = doEdit();
		} else if (_tag.equals(Const.WEB_OP_UPDATE)) {
			redirectPage = doUpdate();
		} else if (_tag.equals(Const.WEB_OP_DELETE)) {
			redirectPage = doDelete();
		} else if (_tag.equals(Const.WEB_OP_PAGE)) {
			redirectPage = doPage();
		} else if (_tag.equals(Const.WEB_OP_ALL)) {
			redirectPage = doAll();
		} else if (_tag.equals(Const.WEB_OP_LIST)) {
			redirectPage = doList();
		} else {
			redirectPage = doNoop();
		}
		doAfter();
		return redirectPage;
	}

	public void doBefore() {
	}

	public void doAfter() {
	}

	public String doView() {
		return Const.WEB_OP_VIEW;
	}

	public String doNew() {
		return Const.WEB_OP_NEW;
	}

	public String doAdd() {
		return Const.WEB_OP_ADD;
	}

	public String doEdit() {
		return Const.WEB_OP_EDIT;
	}

	public String doUpdate() {
		return Const.WEB_OP_UPDATE;
	}

	public String doDelete() {
		return Const.WEB_OP_DELETE;
	}

	public String doPage() {
		return Const.WEB_OP_PAGE;
	}

	public String doAll() {
		return Const.WEB_OP_ALL;
	}

	public String doList() {
		return Const.WEB_OP_LIST;
	}

	public String doNoop() {
		this.addActionError("The parameter passed is wrong.");
		this._msg = "The parameter passed is wrong.";
		return PAGE_ERROR;
	}

	public boolean validateUser() {
		boolean isValid = false;
		String loginUser = (String) this.getRequest().getAttribute(
				Const.WEB_LOGINUSER);
		if (loginUser != null && !loginUser.isEmpty())
			isValid = true;
		return isValid;
	}

	protected void mimicLogin() {
		this.getRequest().setAttribute(Const.WEB_LOGINUSER, Const.FS_USER);
	}
}
